﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Net;

using EHTWebsite.Code;
using EHTWebsiteProject.Code.BLL;
using System.Resources;
using System.Threading;
using System.Globalization;
using System.Text;
using EHTWebsiteProject.Code.Entity;

namespace EHTWebsiteProject.Administrator
{
    public partial class AdminServicesLevel : System.Web.UI.Page
    {

        public Boolean IsVN = true;
        private String languageCode = "vi-VN";
        private String message = "";
        private Int32 SeviceLevelID = HangSo.DEFAULT_ID;
        private ResourceManager rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                try
                {
                    languageCode = Session[HangSo.LANGUAGE_CODE].ToString();
                    Session["NgonNgu"] = languageCode;
                    if (languageCode == "en-us")
                    {
                        IsVN = false;
                    }

                }
                catch (Exception ex)
                {
                }
                try
                {
                    message = Request.QueryString["Message"].ToString();
                    if (message != "")
                        ltrErorr.Text = HelpMethod.ApplySuccess(message);
                }
                catch
                {

                }
                try
                {
                    SeviceLevelID = Int32.Parse(Request.QueryString["SeviceLevelID"].ToString());

                }
                catch
                {

                }


                if (SeviceLevelID != -1)
                {
                    ViewState["action"] = HangSo.ACTION_UPDATE;
                    ViewState["SeviceLevelID"] = SeviceLevelID;
                    ServiceLevels theServices = ServiceLevelsProvider.GetServiceLevelByID(SeviceLevelID);
                    txtServicesCode.Text = theServices.ServiceCost.ToString();
                    txtServicesLevel.Text = theServices.Description.ToString();
                }
                else
                    ViewState["action"] = HangSo.ACTION_ADD;
                Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCode);
                LoadLanguage(Thread.CurrentThread.CurrentCulture);
                LoadServicesLevel();                
            }            
        }

        /// <summary>
        /// Ham load ngon ngu
        /// </summary>
        /// <param name="ci"></param>
        public void LoadLanguage(CultureInfo ci)
        {
            lbDel.Text = rm.GetString("lbDel", ci);

            lbHelp.Text = rm.GetString("lbHelp", ci);                    
        }

        public void LoadServicesLevel()
        {
            rpServicesLevel.DataSource = ServiceLevelsProvider.GetServiceLevels();
            rpServicesLevel.DataBind();
        }

        protected void ImgDel_Click(object sender, ImageClickEventArgs e)
        {
            ArrayList arr = new ArrayList();
            foreach (RepeaterItem rp in rpServicesLevel.Items)
            {
                CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                Label lbDelID = (Label)rp.FindControl("lbDelID");
                if (ckMenu.Checked == true)
                {
                    arr.Add(lbDelID.Text);
                }
            }           

            for (int i = 0; i < arr.Count; i++)
                ServiceLevelsProvider.DeleteServices(Int32.Parse(arr[i].ToString()));
            LoadServicesLevel();
            ltrErorr.Text = "";
        }

        public void Clear()
        {
            txtServicesCode.Text = "";
            txtServicesLevel.Text = "";           

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            String action = ViewState["action"].ToString();

            ServiceLevels theServices = new ServiceLevels();
            theServices.Description = txtServicesLevel.Text;
            try
            {
                theServices.ServiceCost = Int32.Parse(txtServicesCode.Text);
            }
            catch
            {
                ltrErorr.Text = "Tjenestene koste må være nummer";
                return;
            }
            if (action == HangSo.ACTION_ADD)
            {
                theServices.SeviceLevelID = -1;
                ServiceLevelsProvider.IUServiceLevels(theServices, true);
            }
            else
            {
                theServices.SeviceLevelID = Int32.Parse(ViewState["SeviceLevelID"].ToString());
                 ServiceLevelsProvider.IUServiceLevels(theServices, false);
            }
            Clear();
            Response.Redirect("AdminServicesLevel.aspx");
        }

        public void CheckAll(object sender, System.EventArgs e)
        {
            CheckBox ckAll = (CheckBox)sender;
            if (ckAll.Checked)
            {
                foreach (RepeaterItem rp in rpServicesLevel.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    ckMenu.Checked = true;
                }
            }
            else
            {
                foreach (RepeaterItem rp in rpServicesLevel.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    ckMenu.Checked = false;
                }
            }         
        }
    }
}